using System;
using Firestarter.Test.Base;
using FireStarterModeller.BO;
using Habanero.Base;
using Habanero.BO;
using NUnit.Framework;

namespace FireStarterModeller.Test.BO
{
    [TestFixture]
    public class TestDMRelationshipOneDirectionMustBeDoNothing
    {
        [SetUp]
        public void SetupTest()
        {
            //Runs every time that any testmethod is executed
            TestUtilsDomain.SetupTest();
            BORegistry.DataAccessor = new DataAccessorInMemory();
        }

        [TestFixtureSetUp]
        public void TestFixtureSetup()
        {
            //Code that is executed before any test is run in this class. If multiple tests
            // are executed then it will still only be called once.
            TestUtilsDomain.SetupFixture();
        }

        [TearDown]
        public void TearDownTest()
        {
            //runs every time any testmethod is complete
        }

        #region DMRelationshipOneDirectionMustBeDoNothing

//        [Test]
//        public void Test_CreateDMRelationshipManyOneRelationshipMustBeDoNothingRule_WhenClassNull_ShouldRaiseError()
//        {
//            //---------------Set up test pack-------------------
//            //---------------Assert Precondition----------------
//            //---------------Execute Test ----------------------
//            try
//            {
//                new DMRelationshipOneDirectionMustBeDoNothing(null);
//                Assert.Fail("expected ArgumentNullException");
//            }
//                //---------------Test Result -----------------------
//            catch (ArgumentNullException ex)
//            {
//                StringAssert.Contains("Value cannot be null", ex.Message);
//                StringAssert.Contains("dmRelationship", ex.ParamName);
//            }
//        }

        [Test]
        public void Test_CreateDMRelationshipManyOneRelationshipMustBeDoNothingRule()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            DMRelationship relationship = dmClass.OwnerRelationships.CreateBusinessObject();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            IBusinessObjectRule rule = new DMRelationshipOneDirectionMustBeDoNothing();
            //---------------Test Result -----------------------
            Assert.IsNotNull(rule);
            Assert.AreEqual("DMRelationshipOneDirectionMustBeDoNothing", rule.Name);
            Assert.AreEqual(ErrorLevel.Error, rule.ErrorLevel);
        }

        [Test]
        public void Test_CreateDMRelationshipManyOneRelationshipMustBeDoNothingRule_NullClassDM()
        {
            //---------------Set up test pack-------------------
            DMClass dmClass = TestUtilsDMClass.CreateUnsavedDefaultDMClass();
            DMRelationship relationship = dmClass.OwnerRelationships.CreateBusinessObject();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            IBusinessObjectRule rule = new DMRelationshipOneDirectionMustBeDoNothing();
            rule.IsValid(relationship);
            //---------------Test Result -----------------------
            Assert.IsNotNull(rule);
            Assert.AreEqual("DMRelationshipOneDirectionMustBeDoNothing", rule.Name);
            Assert.AreEqual(ErrorLevel.Error, rule.ErrorLevel);

            StringAssert.Contains("The Single Relationship '", rule.Message);
            StringAssert.Contains("'" + relationship + "' on Class '" + relationship.ClassDM + "'", rule.Message);
            StringAssert.Contains("' which is a reverse of the Many Relationship '" + relationship.ReverseRelationship + "'", rule.Message);
            StringAssert.Contains("' on Class '" + relationship.RelatedClass + "' must be 'DoNothing'", rule.Message);
        }

        [Test]
        public void Test_DMRelationshipManyOneRelationshipMustBeDoNothing_WhenBothHaveDeletePrevent_ShouldFail()
        {
            //---------------Set up test pack-------------------
            DMRelationship dmRelationship = new DMRelationship();
            IBusinessObjectRule rule = new DMRelationshipOneDirectionMustBeDoNothing();
            dmRelationship.Cardinality = Cardinality.Single;
            dmRelationship.ReverseRelationship.Cardinality = Cardinality.Multiple;
            dmRelationship.DeleteAction = DeleteParentAction.Prevent;
            dmRelationship.ReverseRelationship.DeleteAction = DeleteParentAction.Prevent;
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmRelationship);
            //---------------Test Result -----------------------
            Assert.IsFalse(isValid);
            TestUtilsShared.AssertStringNotEmpty(rule.Message, "rule.Message");
        }

        [Test]
        public void Test_DMRelationshipManyOneRelationshipMustBeDoNothing_WhenBothHaveDeletePrevent_WhenRelIsOneToMany_ShouldPass()
        {
            //Although this is invalid it will be picked up by the reverse relationship being validated
            //---------------Set up test pack-------------------
            DMRelationship dmRelationship = new DMRelationship();
            IBusinessObjectRule rule = new DMRelationshipOneDirectionMustBeDoNothing();
            dmRelationship.Cardinality = Cardinality.Multiple;
            dmRelationship.ReverseRelationship.Cardinality = Cardinality.Single;
            dmRelationship.DeleteAction = DeleteParentAction.Prevent;
            dmRelationship.ReverseRelationship.DeleteAction = DeleteParentAction.Prevent;
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmRelationship);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        } 
        [Test]
        public void Test_DMRelationshipManyOneRelationshipMustBeDoNothing_WhenOneDoNothing_ShouldPass()
        {
            //---------------Set up test pack-------------------
            DMRelationship dmRelationship = new DMRelationship();
            IBusinessObjectRule rule = new DMRelationshipOneDirectionMustBeDoNothing();
            dmRelationship.Cardinality = Cardinality.Multiple;
            dmRelationship.ReverseRelationship.Cardinality = Cardinality.Single;
            dmRelationship.DeleteAction = DeleteParentAction.DoNothing;
            dmRelationship.ReverseRelationship.DeleteAction = DeleteParentAction.Prevent;
            //---------------Assert Precondition----------------
            Assert.AreEqual(DeleteParentAction.DoNothing,dmRelationship.DeleteAction);
            //---------------Execute Test ----------------------
            bool isValid = rule.IsValid(dmRelationship);
            //---------------Test Result -----------------------
            Assert.IsTrue(isValid);
        }

        #endregion
    }
}